﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Mono.Cecil.Metadata
{
	// Token: 0x020000FD RID: 253
	internal class StringHeapBuffer : HeapBuffer
	{
		// Token: 0x170002BE RID: 702
		// (get) Token: 0x06000A35 RID: 2613 RVA: 0x0002E348 File Offset: 0x0002C548
		public sealed override bool IsEmpty
		{
			get
			{
				return this.length <= 1;
			}
		}

		// Token: 0x06000A36 RID: 2614 RVA: 0x00005A70 File Offset: 0x00003C70
		public StringHeapBuffer() : base(1)
		{
			base.WriteByte(0);
		}

		// Token: 0x06000A37 RID: 2615 RVA: 0x0002E368 File Offset: 0x0002C568
		public virtual uint GetStringIndex(string @string)
		{
			uint num;
			bool flag = this.strings.TryGetValue(@string, out num);
			uint result;
			if (flag)
			{
				result = num;
			}
			else
			{
				num = (uint)(this.strings.Count + 1);
				this.strings.Add(@string, num);
				result = num;
			}
			return result;
		}

		// Token: 0x06000A38 RID: 2616 RVA: 0x0002E3B0 File Offset: 0x0002C5B0
		public uint[] WriteStrings()
		{
			List<KeyValuePair<string, uint>> list = StringHeapBuffer.SortStrings(this.strings);
			this.strings = null;
			uint[] array = new uint[list.Count + 1];
			array[0] = 0U;
			string text = string.Empty;
			foreach (KeyValuePair<string, uint> keyValuePair in list)
			{
				string key = keyValuePair.Key;
				uint value = keyValuePair.Value;
				int position = this.position;
				bool flag = text.EndsWith(key, StringComparison.Ordinal) && !StringHeapBuffer.IsLowSurrogateChar((int)keyValuePair.Key[0]);
				if (flag)
				{
					array[(int)value] = (uint)(position - (Encoding.UTF8.GetByteCount(keyValuePair.Key) + 1));
				}
				else
				{
					array[(int)value] = (uint)position;
					this.WriteString(key);
				}
				text = keyValuePair.Key;
			}
			return array;
		}

		// Token: 0x06000A39 RID: 2617 RVA: 0x0002E4B0 File Offset: 0x0002C6B0
		private static List<KeyValuePair<string, uint>> SortStrings(Dictionary<string, uint> strings)
		{
			List<KeyValuePair<string, uint>> list = new List<KeyValuePair<string, uint>>(strings);
			list.Sort(new StringHeapBuffer.SuffixSort());
			return list;
		}

		// Token: 0x06000A3A RID: 2618 RVA: 0x0002E4D8 File Offset: 0x0002C6D8
		private static bool IsLowSurrogateChar(int c)
		{
			return c - 56320 <= 1023;
		}

		// Token: 0x06000A3B RID: 2619 RVA: 0x00005A93 File Offset: 0x00003C93
		protected virtual void WriteString(string @string)
		{
			base.WriteBytes(Encoding.UTF8.GetBytes(@string));
			base.WriteByte(0);
		}

		// Token: 0x04000423 RID: 1059
		protected Dictionary<string, uint> strings = new Dictionary<string, uint>(StringComparer.Ordinal);

		// Token: 0x020000FE RID: 254
		private class SuffixSort : IComparer<KeyValuePair<string, uint>>
		{
			// Token: 0x06000A3C RID: 2620 RVA: 0x0002E4FC File Offset: 0x0002C6FC
			public int Compare(KeyValuePair<string, uint> xPair, KeyValuePair<string, uint> yPair)
			{
				string key = xPair.Key;
				string key2 = yPair.Key;
				int num = key.Length - 1;
				int num2 = key2.Length - 1;
				while (num >= 0 & num2 >= 0)
				{
					bool flag = key[num] < key2[num2];
					int result;
					if (flag)
					{
						result = -1;
					}
					else
					{
						bool flag2 = key[num] > key2[num2];
						if (!flag2)
						{
							num--;
							num2--;
							continue;
						}
						result = 1;
					}
					return result;
				}
				return key2.Length.CompareTo(key.Length);
			}
		}
	}
}
